package com.zhuiYun.project.api.customer.controller;

import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.zhuiYun.project.api.customer.entity.Customer;
import com.zhuiYun.project.api.customer.entity.FriendEntity;
import com.zhuiYun.project.api.customer.entity.MsgDTO;
import com.zhuiYun.project.api.customer.service.CustomerService;
import com.zhuiYun.project.common.controller.BaseController;
import com.zhuiYun.project.common.response.CommonResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 客服
 * LC: 2023-04-03 14:04
 */
@RestController
@RequestMapping("/customer")
@CrossOrigin
public class CustomerController extends BaseController {

    @Autowired
    CustomerService customerService;

    /**
    * @Description 保存聊天记录
    * @Author lzy
    * @Date 2023/4/20 11:29
    * @Return com.zhuiYun.project.common.response.CommonResult
     * @param customer
    */
    @PostMapping("/saveChatRecord")
    public CommonResult saveChatRecord(@RequestBody Customer customer){
        List<MsgDTO> msgVos = JSON.parseArray(customer.getMsg(), MsgDTO.class);
        String userId = (String) JSON.parse(customer.getUserId());
        return customerService.saveChatRecord(msgVos, userId);
    }

    /**
    * @Description 根据用户id获取聊天记录
    * @Author lzy
    * @Date 2023/4/20 11:35
    * @Return com.zhuiYun.project.common.response.CommonResult
    * @param userId
    */
    @GetMapping("/getChatRecordByUserId")
    public CommonResult getChatRecordByUserId(String userId){
        return customerService.getChatRecordByUserId(userId);
    }

    /**
     * 新增会话
     * LC: 2023-04-04 18:37
     * @param id
     * @return
     */
    @PostMapping("/saveFriend")
    @ApiOperation(value = "新增会话")
    public CommonResult saveFriend(Integer id) {
        return CommonResult.ok(customerService.saveFriend(id));
    }


    /**
     * @descriptions 保存聊天记录
     * LC: 2023-04-04 13:50
     */
    @PostMapping("/saveMessageRecord")
    @ApiOperation(value = "保存聊天记录")
    public CommonResult saveMessageRecord(@RequestBody Map<String,String> param) {
        // 判断param是否为空
        if (param != null) {
            // 取值
            String sender = param.get("sender");
            String persons = param.get("persons");
            String msg = param.get("msg");
            // 保存聊天记录
            customerService.storeMessage(sender, persons, msg);
            // 返回结果
            return CommonResult.ok("保存成功");
        } else {
            // param为空返回结果
            return CommonResult.ok("param为空");
        }
    }

    /**
     * 查询聊天记录
     * LC: 2023-04-04 13:51
     */
    @GetMapping("/readMessage")
    @ApiOperation(value = "查询聊天记录")
    public CommonResult readMessage(Integer myId, Integer friendId){
        FriendEntity friend = customerService.getFriend(myId,friendId);
        List<JSONObject> messageList = customerService.readMessage(friend.getSessionId());
        return CommonResult.ok(messageList);
    }

    /**
     * @descriptions 获取客服信息
     * LC: 2023-04-04 13:52
     * @param myId
     * @return result.CommonResult
     */
    @GetMapping("/getFriendMapperList")
    @ApiOperation(value = "获取客服信息")
    public CommonResult getFriendMapperList(Integer myId){
        if (myId != null) {
            List<JSONObject> friendList = customerService.getFriendList(myId);
            return CommonResult.ok(friendList);
        }else {
            return CommonResult.ok("id为空");
        }
    }

    /**
     * 清空读取信息
     * LC: 2023-04-04 13:53
     * @param myId
     * @param friendId
     * @return
     */
    @GetMapping("emptyUnRead")
    @ApiOperation(value = "清空读取信息")
    public CommonResult emptyUnRead(Integer myId, Integer friendId){
        if (myId != null && friendId != null) {
            customerService.emptyUnRead(myId, friendId);
            return CommonResult.ok("读取成功");
        }else {
            return CommonResult.ok("空");
        }
    }


    /**
     * 查询会话列表
     * LC: 2023-04-04 14:48
     * @param myId
     * @param friendId
     * @return
     */
    @GetMapping("/friendById")
    @ApiOperation(value = "查询会话列表")
    public CommonResult friendById(Integer myId, Integer friendId){
        FriendEntity friend = customerService.getFriend(myId,friendId);
        return CommonResult.ok(friend);
    }

}
